﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Microsoft.Services.NuGet.Server.Infrastructure
{
    public class BuildPackagePathResolver
        : IPackagePathResolver 
    {
        private readonly BuildPackageStore _store;

        public BuildPackagePathResolver(Uri teamProjectCollectionUrl)
        {
            _store = new BuildPackageStore(teamProjectCollectionUrl);
        }

        public string GetInstallPath(IPackage package)
        {
            string packageName = GetPackageFileName(package);

            foreach (string packageUri in _store.Packages)
            {
                if (packageUri.EndsWith(packageName, StringComparison.OrdinalIgnoreCase))
                {
                    return _store.GetFullPath(packageUri);
                }
            }

            return null;
        }

        public string GetPackageDirectory(IPackage package)
        {
            return null;
        }

        public string GetPackageFileName(IPackage package)
        {
            return package.Id + "." + package.Version + Constants.PackageExtension;
        }
    }
}